#!/usr/bin/python
# -*- coding: utf-8 -*-

import glob
import sys
import os
import re
import string
import pepe.p_txt.bigtxt as bt
import pepe.glib as g
import pepe.p_filesystem.basic as pfb

fslist = lambda x, y: pfb.fslist(x, patterns=y, recurse=True, return_folders=False)

n = '\x0D\x0A'

unwanted_suffixes = ['xls', 'xlsx', 'zip', 'tar', 'tgz', 'gz', 'bz2', 
'doc', 'docx', 'pdf', 'psd', 'pst', 'ppt', 'jpg', 'jpeg', 'png', 'gif', 
'tif', 'mdb', 'mdbx', 'pyc', 'pyw']

def main(inpath, filemask, rows2take="2", skip_other_extensions=""):
    ''' '''
    files = fslist(inpath + '\x5C', filemask)
    for s in skip_other_extensions.split(','):
        unwanted_suffixes.append(s)

    # 2010-10-22_2257 vp
    ## using underscore instead dot because .# utilities 
    ## cut out everything after dot and then is not clear 
    ## whether utility was run on original file or on output
    ## of other .# utility that processed given file
    o = open('_tlh.#', 'wb')
    #o = open('tlh_output.#', 'wb')
    s = open('_tlh_skipped.#', 'wb')
    o.write('_\t_\t_' + n)
    
    for f in files:

        lino = 0
        print f

        dot_pos = f.rfind('.')
        if dot_pos == -1:                         # searching for file suffix
            suffix = f                             # in case of no suffix
        else:
            suffix = f[f.rfind('.') + 1:].lower()

        if not suffix in unwanted_suffixes:      # file seems to be text file
            for line in bt.lines(f):             # listing the rows2take lines

                line = re.sub('\x0D', '', line)
                line = re.sub('\x0A', '', line)
                line = re.sub('\x09', '', line)

                lino += 1
                o.write("%s\t%s\t%s%s" % (line, str(lino), f, n))

                if str(lino) == rows2take: break
        else:
            s.write("%s\t%s%s" % (f, suffix, n))

    o.close()
    s.close()

if __name__ == "__main__":
    ''' '''
    if len(sys.argv) == 3:
        main(   inpath=sys.argv[1], 
                filemask=sys.argv[2])

    elif len(sys.argv) == 4:
        main(   inpath=sys.argv[1], 
                filemask=sys.argv[2], 
                rows2take=sys.argv[3])

    elif len(sys.argv) == 5:
        main(   inpath=sys.argv[1], 
                filemask=sys.argv[2], 
                rows2take=sys.argv[3], 
                skip_other_extensions=sys.argv[4])
    else:
        print """

        Traverse and List Headers (or Text List Headers) utility
        Take defined number lines from each file to distinguish text headers

        Utility:

            tlh.py "\\server\path\directory" *
            tlh.py "\\server\path\directory" *.* 3
            tlh.py "\\server\path\directory" * 3 abc,def,ghi

        Library:

            import pepe.tlh as t
            t.main(inpath, filemask, rows2take='2', skip_other_extensions='')
            
            inpath                  path to scan
            filemask                file name regexp definition
            rows2take               how many rows to take from each file
            skip_other_extensions   other extensions to skip (separated by ,)
                                    by default are skipped following extensions
            """, 
        for us in unwanted_suffixes:
            print us, # '\t'*4
        print n, n
